package com.yml.mall.category;

import com.yml.mall.category.pojo.CategoryInsertDTO;
import com.yml.mall.category.pojo.CategoryStructureDTO;
import com.yml.mall.vo.ResponseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @ClassName CategoryCotroller
 * @Author Administrator
 * @Date 2020/4/8   17:34
 */
@Api(tags = "分类Api")
@RestController
@Validated
public class CategoryController {
    private    final  CategoryService categoryService;
    public CategoryController(CategoryServiceImpl categoryService){
        this.categoryService=categoryService;
    }

    @ApiOperation("获取完整目录")
    @RequestMapping("/categoryStructure")
    public ResponseVO<List<CategoryStructureDTO>> categoryStructure(){
        //List<CategoryStructureDTO> root=new ArrayList<>();
        //categoryService.getCategoryStructure(root,categoryService.getAllRootItem().getData());
        //return  ResponseVO.success(root);
        return categoryService.CategoryStructureDTO();
    }

    /**
     * 获取{id}下的子目录目录
     * @return
     */
    @ApiOperation("获取子目录")
    @ApiImplicitParam(paramType = "path",name = "id",dataType = "Integer",required = true,value = "类目id")
    @GetMapping("/category/{id}")
    public ResponseVO<List<CategoryStructureDTO>> rootItem(@PathVariable(value = "id")  Integer parentId){

        return categoryService.getSubItem(parentId);
    }

    @ApiOperation("增加目录")
    @PostMapping("/addCategory")
    public ResponseVO<List<CategoryStructureDTO>> addCategory(@RequestBody @Valid CategoryInsertDTO categoryInsertDTO){
        return categoryService.save(categoryInsertDTO);
    }

}
